define([
    'jquery',
    'underscore',
    'backbone',
    'text!login/login.html',
    'js/configAmd',
    'js/myUtilsAmd',
    'js/myApiAmd',


], function ($, _, Backbone, tpl, config, myUtils, myApiAmd) {
    console.log('this is login view');

    var View = Backbone.View.extend({

        render: function () {
            console.log('render');
            var tmpl = _.template(tpl);
            $(this.el).html(tmpl);
            return this;
        },

        onRender: function (qs, callback) {
            this.render();
            if (callback)
                callback(this);
        },

        onReshow: function (qs, callback, forceReload) {
            $(this.el).show();
        },

        hide: function () {
            $(this.el).hide();
        },

        remove: function () {
            $(this.el).remove();
        },

        events: {
            "submit #login": "doLogin"
        },

        doLogin: function (event) {
            var formData = $('form#login').serializeObject();
            console.log(formData);
            var self = this;

            var nodeTip = $(self.el).find('#tips');
            nodeTip.hide();

            myApiAmd.login(formData, function (err, res) {
                if (err) {
                    nodeTip.text(err);
                    nodeTip.show();
                } else {
                    console.log('succ', res);
                    window.location.href = "#admin/index.html";
                }
            });
            return false;
        }
    });

    return View;
});